﻿Public Partial Class DettaglioNews
    Inherits System.Web.UI.Page
    Public docsPath As String = ConfigurationManager.AppSettings("docsPath").Replace("~", String.Empty)
    Public docExt As String = ConfigurationManager.AppSettings("docExt")
    Public maxDocSize As Integer = CInt(ConfigurationManager.AppSettings("maxDocSize"))
    Public IDNews As Guid = Nothing
    Private cmnhelp As New Helpers.Common
    Private strhelp As New Helpers.Strings
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not String.IsNullOrEmpty(Request.QueryString("id")) Then
                Page.Title = ConfigurationManager.AppSettings("sitename") & " Amministrazione - Modifica News"
                IDNews = New Guid(Request.QueryString("id"))
                Dim news As News = DAL.Context.News.Where(Function(n) n.ID = IDNews).Single
                If news IsNot Nothing Then
                    With news
                        txtTitolo.Text = .Titolo
                        txtDescrizione.Value = .Testo
                        txtDataPubblicazione.Text = .DataPubblicazione
                        txtDataScadenza.Text = .DataScadenza
                        rpDocumenti.DataSource = .NewsAllegati.ToList
                        rpDocumenti.DataBind()
                    End With
                End If
            Else
                Page.Title = ConfigurationManager.AppSettings("sitename") & " Amministrazione - Nuova News"
                txtDataPubblicazione.Text = Date.Now.ToShortDateString
                IDNews = Guid.NewGuid
            End If
            hfIDNews.Value = IDNews.ToString
        End If
    End Sub
    Private Sub btnsalva_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsalva.Click
        Try
            Dim news As News = Nothing
            If Not String.IsNullOrEmpty(Request.QueryString("id")) Then
                IDNews = New Guid(Request.QueryString("id"))
                news = DAL.Context.News.Where(Function(n) n.ID = IDNews).Single
            Else
                IDNews = New Guid(hfIDNews.Value)
                news = New News
                news.ID = IDNews
                DAL.Context.News.InsertOnSubmit(news)
            End If
            With news
                If Not String.IsInterned(txtTitolo.Text) Then .Titolo = txtTitolo.Text.Trim
                If Not String.IsNullOrEmpty(txtDataPubblicazione.Text) Then .DataPubblicazione = Date.Parse(txtDataPubblicazione.Text.Trim)
                If Not String.IsNullOrEmpty(txtDataScadenza.Text) Then .DataScadenza = Date.Parse(txtDataScadenza.Text.Trim)
                If Not String.IsNullOrEmpty(txtDescrizione.Value) Then .Testo = txtDescrizione.Value
            End With
            'Inserimento nel DB di immagini GIA' CARICATE nel file system. 
            'Dobbiamo solo associarle all'annuncio
            If Not String.IsNullOrEmpty(hfAllegati.Value) Then
                Dim allegati As String() = hfAllegati.Value.Split(",")
                For Each s As String In allegati
                    If Not String.IsNullOrEmpty(s) Then
                        Dim a As New NewsAllegati With {.ID = Guid.NewGuid, .IDNews = IDNews, .Descrizione = String.Empty, .Percorso = s}
                        DAL.Context.NewsAllegati.InsertOnSubmit(a)
                    End If
                Next
            End If
            DAL.Context.SubmitChanges()
            Response.Redirect(Page.ResolveUrl("~/Administrator/ElencoNews.aspx"))
        Catch tex As Threading.ThreadAbortException
            '
        Catch ex As Exception
            cmnhelp.OpenDialog(ex.Message)
        End Try
    End Sub
End Class